package com.csw.scala

object Demo09Apply {
  def main(args: Array[String]): Unit = {
    /**
      * 不使用new创建对象，实际上是调用伴生对象的apply函数
      *
      */
    val dog = Dog("小狗", 2)
    println(dog)
  }
}

class Dog(name:String,age:Int){
}

//伴生对象
object Dog{
  def fun(): Unit = {
    println("dog")
  }

  /**
    * apply函数可以直接通过对象名+括号调用
    *
    */
  def apply(name: String, age: Int): Dog ={
    val dog=new Dog(name, age)
    return dog
  }
}
